package com.lv.phuotbacnam.bl.facebook;

import android.app.Activity;
import android.location.Location;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.model.GraphUser;
import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.network.util.location.LocationHelper;
import com.lv.phuotbacnam.bl.queries.QueriesConstant.TableUser;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseGeoPoint;
import com.parse.ParseUser;
import com.parse.SaveCallback;

/**
 * Created by VanQuan on 02/09/2014.
 */
@SuppressWarnings("deprecation")
public class ParseFacebookHelper {

	public ParseFacebookHelper() {
		super();
	}

	/**
	 * To login facebook with parse facebook utils
	 * 
	 * @param activity
	 * @param callback
	 */
	public void logInFbWithParse(final Activity activity,
			final CallBackFromParse<Object, Object> callback) {
		ParseFacebookUtils.logIn(FacebookConstants.FACEBOOK_PERMISSIONS,
				activity, new LogInCallback() {
					@Override
					public void done(ParseUser parseUser, ParseException e) {
						if (parseUser != null) {
							getFaceBookGraphObject(parseUser, activity,
									new CallBackFromParse<Object, Object>() {
										@Override
										public void done(Object request,
												Object result) {
											callback.done("", "");
										}
									});
						}
					}
				});

	}

	/**
	 * Linking with facebook
	 * 
	 * @param parseUser
	 * @param activity
	 * @param callback
	 */
	private void getFaceBookGraphObject(final ParseUser parseUser,
			final Activity activity,
			final CallBackFromParse<Object, Object> callback) {
		Session session = ParseFacebookUtils.getSession();

		Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

			@Override
			public void onCompleted(GraphUser user, Response response) {
				if (user != null) {
					createUserData(parseUser, activity, user,
							new CallBackFromParse<Object, Object>() {
								@Override
								public void done(Object request, Object result) {
									callback.done("", "");
								}
							});
				}

			}
		});
	}

	/**
	 * To update data for ParseUser
	 * 
	 * @param parseUser
	 * @param activity
	 * @param user
	 * @param callback
	 */
	private void createUserData(final ParseUser parseUser, Activity activity,
			GraphUser user, final CallBackFromParse<Object, Object> callback) {
		String email = user.getProperty("email").toString();
		String facebookId = user.getId();
		String firstName = user.getFirstName();
		String lastName = user.getLastName();

		parseUser.setUsername(email);
		parseUser.setPassword(facebookId);
		parseUser.setEmail(email);

		// other fields can be set just like with ParseObject
		Location lc = new LocationHelper(activity).getLocation();
		if (lc != null) {
			ParseGeoPoint geoPoint = new ParseGeoPoint(lc.getLatitude(),
					lc.getLongitude());
			parseUser.put(TableUser.COLUMN_location, geoPoint);
		}
		parseUser.put(TableUser.COLUMN_firstName, firstName);
		parseUser.put(TableUser.COLUMN_lastName, lastName);
		parseUser.put(TableUser.COLUMN_photoURL,
				FacebookConstants.FACEBOOK_PROFILE_PICTURE_URL.replace("###",
						facebookId));
		parseUser.put(TableUser.COLUMN_facebookId, facebookId);

		parseUser.saveInBackground(new SaveCallback() {
			@Override
			public void done(ParseException e) {
				callback.done("", "");
			}
		});
	}
}